<?php
class ChiphikhoasController extends AppController {

	var $name = 'Chiphikhoas';
	var $uses=array('Chiphikhoa','Tgdongdoanphikhoa','Thang','Chidoansv','Namhoc','Khoa');
	var $helpers = array('ctable','FlashChart');
	function index() {
		$this->Chiphikhoa->recursive = 0;
		$this->set('chiphikhoas', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid chiphikhoa', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('chiphikhoa', $this->Chiphikhoa->read(null, $id));
	}

	function add($khoa_id=null)
	{
		if(!$khoa_id)
		{
			$this->redirect(array('controller'=>'pages','action'=>'home'));
		}
		
		if (!empty($this->data))
		{
			$this->data['Chiphikhoa']['khoa_id']=$khoa_id;
			$this->data['Chiphikhoa']['ngaynhan']=date('Y-m-d',strtotime($this->data['Chiphikhoa']['ngaynhan']));
			$this->Chiphikhoa->create();
			$this->data['Chiphikhoa']['sotien']= str_replace('.','', $this->data['Chiphikhoa']['sotien']);
				
			if ($this->Chiphikhoa->save($this->data))
			{
				$this->Session->setFlash(__('Thông tin đã lưu', true),'success');
				$this->redirect(array('action' => 'thuchi',$khoa_id,$this->Session->read('namhocht_id')));
			}
			else
			{
				$this->Session->setFlash(__('Không Thể Lưu. Vui Lòng Thử Lại', true),'error');
			}
		}
		$this->Chiphikhoa->Loaichiphi->displayField='tenloaichiphi';
		$loaichiphis = $this->Chiphikhoa->Loaichiphi->find('list');
		
		$this->set(compact('loaichiphis', 'khoa_id'));
	}

	function edit($id = null,$khoa_id=null)
	{
		if(!$id)
			$this->redirect(array('controller'=>'pages','action'=>'home'));
		
			
		if (!$id && empty($this->data))
		{
			$this->Session->setFlash(__('Không có Hoạt Động Này', true));
			$this->redirect(array('action' => 'thuchi',$khoa_id,$this->Session->read('namhocht_id')));
		}
		if (!empty($this->data)) {
			$this->data['Chiphikhoa']['khoa_id']=$khoa_id;
			$this->data['Chiphikhoa']['ngaynhan']=date('Y-m-d',strtotime($this->data['Chiphikhoa']['ngaynhan']));
			$this->data['Chiphikhoa']['sotien']= str_replace('.','', $this->data['Chiphikhoa']['sotien']);
			if ($this->Chiphikhoa->save($this->data)) {
				$this->Session->setFlash(__('Thông tin đã lưu', true),'success');
				$this->redirect(array('action' => 'thuchi',$khoa_id,$this->Session->read('namhocht_id')));
			} else {
				$this->Session->setFlash(__('Không Thể Lưu. Vui Lòng Thử Lại', true),'error');
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Chiphikhoa->read(null, $id);			
			$this->data['Chiphikhoa']['ngaynhan'] =date('d-m-Y',strtotime($this->data['Chiphikhoa']['ngaynhan']));
			$this->data['Chiphikhoa']['sotien'] = number_format($this->data['Chiphikhoa']['sotien'],0,',','.');
		}
		$this->Chiphikhoa->Loaichiphi->displayField='tenloaichiphi';
		$loaichiphis = $this->Chiphikhoa->Loaichiphi->find('list');
		$this->set(compact('loaichiphis','khoa_id','id'));
	}

	function delete($khoa_id=null,$namhoc=null,$id = null) {
		if (!$id || !$khoa_id || !$namhoc) {
			$this->Session->setFlash(__('Giá trị không tồn tại', true));
			$this->redirect(array('controller'=>'pages','action'=>'home'));
		}
		if ($this->Chiphikhoa->delete($id)) {
			$this->Session->setFlash(__('Thông tin đã xóa', true),'success');
			$this->redirect(array('action' => 'thuchi',$khoa_id,$namhoc));
		}
		$this->Session->setFlash(__('Không thể xóa. Xin vui lòng thử lại', true),'error');
		$this->redirect(array('action' => 'thuchi',$khoa_id,$namhoc));
	}

	function thuchi($khoa_id=null,$namhoc=null)
	{
		if(!$khoa_id)
			$this->redirect(array('controller'=>'pages','action'=>'home'));
			
			$namhocs = $this->Namhoc->find('list',array('fields'=>array('id','namhoc')));
			if(!$namhoc)
				$namhoc = $this->Session->read('namhocht_id');
			/*
			 * thong tin nam hoc va chi doan
			 */
			$thangs=$this->Thang->find('list',array('fields'=>array('id','thang'),
                'conditions'=>array('namhoc_id'=>$namhoc),
				'order'=>array('id ASC')));
			if(!empty($thangs))
			{
        		$thang_id = array_keys($thangs);
			}
       		else
       		{
        		$thang_id= array();
       		}
			
			$chidoansv=$this->Chidoansv->find('first',array(
                'conditions'=>array('Chidoansv.id'=>$khoa_id),
                'fields'=>array('Chidoansv.id','tenchidoan','Khoa.tenkhoa','khoahoc_id'),
                'recursive'=>0));
			
			$khoa=$this->Khoa->find('first',array(
                'conditions'=>array('Khoa.id'=>$khoa_id),
                'recursive'=>-1));
			
			$nhoc = $this->Namhoc->find('list',array('conditions'=>array('id'=>$namhoc),
        									'fields'=>array('id','namhoc')));
		
			/*
			 * tao data thu doan phi nam hoc
			 */
			/*$querystr = "select sum(a.dadong * sotien) as doanphi
							from (SELECT Tgdongdoanphisv.thang_id,count(`Tgdongdoanphisv`.`sinhvien_id`) as dadong, Doanphisv.sotien
								FROM `tgdongdoanphisvs` AS `Tgdongdoanphisv`,`doanphisvs` as `Doanphisv`
								WHERE Tgdongdoanphisv.thang_id = Doanphisv.thang_id
								AND `Tgdongdoanphisv`.`namhoc_id` =".$namhoc."
								AND `Tgdongdoanphisv`.`chidoansv_id` =".$khoa_id."
								group by Tgdongdoanphisv.thang_id) a";*/
			
			$querystr = "	select sum(Tgdongdoanphikhoa.sotien) as doanphi
							from tgdongdoanphikhoas as Tgdongdoanphikhoa
							where Tgdongdoanphikhoa.khoa_id=".$khoa_id."
							and  namhoc_id=".$namhoc;
			$doanphinht = $this->Chiphikhoa->query($querystr);
			/*
			 * tao data thu doan phi tong hop
			 */
			/*$querystr = "select sum(sotien) as doanphi
							from (SELECT Tgdongdoanphisv.thang_id,count(`Tgdongdoanphisv`.`sinhvien_id`) as dadong, Doanphisv.sotien
								FROM `tgdongdoanphisvs` AS `Tgdongdoanphisv`,`doanphisvs` as `Doanphisv`
								WHERE Tgdongdoanphisv.thang_id = Doanphisv.thang_id
								AND `Tgdongdoanphisv`.`chidoansv_id` =".$khoa_id."
								group by Tgdongdoanphisv.thang_id) a";*/
			
			$querystr = "	select sum(Tgdongdoanphikhoa.sotien) as doanphi
							from tgdongdoanphikhoas as Tgdongdoanphikhoa
							where Tgdongdoanphikhoa.khoa_id=".$khoa_id;
			$doanphith = $this->Chiphikhoa->query($querystr);
			
        	/*
        	 * tao ngay bat dau den ngay ket thuc
        	 */
        	if(!empty($thangs))
        	{
	        	$nams = split('-',$nhoc[key($nhoc)]);
				$thang_d = split('-',$thangs[$thang_id[0]]);
	        	$thang_dau  =$thang_d[0];
				$thang_c = split('-',$thangs[$thang_id[count($thang_id)-1]]);;
	        	$thang_cuoi = $thang_c[0];
	        	$ngaybd =date('Y-m-d',strtotime($nams[0].'-'.$thang_dau.'-1'));
	        	$ngaykt='';
	        	for($i=31;$i>=26;$i--)
	        	{
	        		if(checkdate($thang_cuoi, $i, $nams[1]))
	        		{
	        			$ngaykt = date('Y-m-d',strtotime($nams[1].'-'. $thang_cuoi.'-'.$i));
	        			break;
	        		}
	        	}
	        	/*
	        	 * phi thu - chi nam hoc
	        	 */
	        	$phicdsvs = $this->Chiphikhoa->find('all',array(
	        									'fields'=>array('loaichiphi_id','sum(Chiphikhoa.sotien) as sotien'),
	        									'conditions'=>array('ngaynhan >='=>$ngaybd,
	        														'ngaynhan <='=>$ngaykt,
	        														'Chiphikhoa.khoa_id'=>$khoa_id),
	        									'group'=>array('loaichiphi_id')));
	        	
	        	/*
	        	 * phi thu - chi tong hop
	        	 */
	        	$phicdsvths = $this->Chiphikhoa->find('all',array(
	        									'fields'=>array('loaichiphi_id','sum(Chiphikhoa.sotien) as sotien'),
	        									'conditions'=>array('Chiphikhoa.khoa_id'=>$khoa_id),
	        									'group'=>array('loaichiphi_id')));
	        	/*
	        	 * tao data cho table
	        	 */
	        	$cols=array();
	        	$rows=array();
	        	$data=array();
	        	// cols
	        	$cols[0]='Tổng hợp';
	        	$cols[1]='Năm học '.$nhoc[key($nhoc)];	        	
	        	//row
	        	$rows[0]='Tiền Thu Đoàn Phí';
	        	$rows[1]='Tiền Thu Khác';
	        	$rows[2]='Tổng Thu';
	        	$rows[3]='Tổng Chi';
	        	$rows[4]='Số Dư';
	        	
	        	$chi = 0;
	        	$thu = 0;
	        	foreach ($phicdsvs as $phicdsv) 
	        	{
	        		if($phicdsv['Chiphikhoa']['loaichiphi_id']==3)
	        			$thu=$phicdsv[0]['sotien'];
	        		else
	        			$chi+=$phicdsv[0]['sotien'];	        		
	        	}
	        	//tong hop
	        	$chith = 0;
	        	$thuth = 0;
        		foreach ($phicdsvths as $phicdsvth) 
	        	{
	        		if($phicdsvth['Chiphikhoa']['loaichiphi_id']==3)
	        			$thuth=$phicdsvth[0]['sotien'];
	        		else
	        			$chith+=$phicdsvth[0]['sotien'];	        		
	        	}
	        	$dpth = $doanphith[0][0]['doanphi'];
	        	$dpht =$doanphinht[0][0]['doanphi'];
	        	$thth = (empty($thuth)?0:$thuth);
	        	$chth =(empty($chith)?0:$chith);
	        	$data[0][0]=number_format($dpth,0,',','.');
	        	$data[0][1]=number_format($dpht,0,',','.');
	        	//tong hop
	        	$data[1][0] = number_format($thth,0,',','.');
	        	$data[2][0] = number_format($dpth + $thth,0,',','.');
	        	$data[3][0] = number_format($chth,0,',','.');
	        	$data[4][0] = number_format($dpth + $thth-$chth,0,',','.');
	        	//nam hoc ht
	        	$data[1][1] = number_format($thu,0,',','.');
	        	$data[2][1] = number_format($dpht +$thu,0,',','.');
	        	$data[3][1] = number_format($chi,0,',','.');
	        	$data[4][1] = number_format($dpht +$thu-$chi,0,',','.');
	        	
	        	$this->Chiphikhoa->recursive = 0;
				$this->set('chiphikhoas', $this->paginate(array('ngaynhan >='=>$ngaybd,'ngaynhan <='=>$ngaykt,'Chiphikhoa.khoa_id'=>$khoa_id)));
        	}
        	$this->set(compact('rows','cols','data','khoa_id','namhoc','namhocs','dpht'));
	}
	
	function updatethuchi($khoa_id=null)
	{
		$this->layout='ajax';
		$namhoc = $this->data['Chiphikhoa']['namhoc_id'];
		$nhoc = $this->Namhoc->find('list',array('conditions'=>array('id'=>$namhoc),
        									'fields'=>array('id','namhoc')));
		$thangs=$this->Thang->find('list',array('fields'=>array('id','thang'),
                'conditions'=>array('namhoc_id'=>$namhoc),
				'order'=>array('id ASC')));
		if(!empty($thangs))
		{
        	$thang_id = array_keys($thangs);
		}
       	else
       	{
        	$thang_id= array();
       	}
       	if(!empty($thangs))
       	{
			$nams = split('-',$nhoc[key($nhoc)]);
		   $thang_d = split('-',$thangs[$thang_id[0]]);
	        	$thang_dau  =$thang_d[0];
				$thang_c = split('-',$thangs[$thang_id[count($thang_id)-1]]);;
	        	$thang_cuoi = $thang_c[0];
	        	$ngaybd =date('Y-m-d',strtotime($nams[0].'-'.$thang_dau.'-1'));
	        	$ngaykt='';
		    for($i=31;$i>=26;$i--)
		    {
		    	if(checkdate($thang_cuoi[0],$i, $nams[1]))
				{
		        	$ngaykt = date('Y-m-d',strtotime($nams[1].'-'. $thang_cuoi[0].'-'.$i));
		        	break;
		        }
		    }
		    $querystr = "	select sum(Tgdongdoanphikhoa.sotien) as doanphi
							from tgdongdoanphikhoas as Tgdongdoanphikhoa
							where Tgdongdoanphikhoa.khoa_id=".$khoa_id."
							and  namhoc_id=".$namhoc;
			$doanphinht = $this->Chiphikhoa->query($querystr);
			$dpht = $doanphinht[0][0]['doanphi'];
			$this->Chiphikhoa->recursive = 0;
			$this->set('chiphikhoas', $this->paginate(array('ngaynhan >='=>$ngaybd,'ngaynhan <='=>$ngaykt,'Chiphikhoa.khoa_id'=>$khoa_id)));
			$this->set(compact('khoa_id','namhoc','dpht'));
       	}
	}

	function tkthuchi($khoa_id=null,$ngaybd=null,$ngaykt=null)
	{
		if(!empty($this->data))
		{
			if($this->data['Chiphikhoa']['ngaybd']=='')
				$this->data['Chiphikhoa']['ngaybd'] = date('d-m-Y');
			if($this->data['Chiphikhoa']['ngaykt']=='')
				$this->data['Chiphikhoa']['ngaykt'] = date('d-m-Y');
			$ngaybd = date('Y-m-d',strtotime($this->data['Chiphikhoa']['ngaybd']));
			$ngaykt = date('Y-m-d',strtotime($this->data['Chiphikhoa']['ngaykt']));
			$this->redirect(array('controller'=>'chiphikhoas','action'=>'tkthuchi',$khoa_id,$ngaybd,$ngaykt));
		}
		if(!empty($ngaybd))
			$this->data['Chiphikhoa']['ngaybd'] = date('d-m-Y',strtotime($ngaybd));
		if(!empty($ngaykt))
			$this->data['Chiphikhoa']['ngaykt'] = date('d-m-Y',strtotime($ngaykt));
		
		/*$phicdsvs = $this->Chiphikhoa->find('all',array(
	        									'fields'=>array('loaichiphi_id','sum(Chiphikhoa.sotien) as sotien'),
	        									'conditions'=>array('ngaynhan >='=>$ngaybd,
	        														'ngaynhan <='=>$ngaykt,
	        														'Chiphikhoa.khoa_id'=>$khoa_id),
	        									'group'=>array('loaichiphi_id')));*/
		$querystr = "	select sum(Tgdongdoanphikhoa.sotien) as doanphi
							from tgdongdoanphikhoas as Tgdongdoanphikhoa
							where Tgdongdoanphikhoa.khoa_id=".$khoa_id."
							and  ngaydong >='".$ngaybd."' and ngaydong <='".$ngaykt."'";
		$doanphinht = $this->Chiphikhoa->query($querystr);
		
		$dpht = isset($doanphinht[0][0]['doanphi'])?$doanphinht[0][0]['doanphi']:0;
		$this->Chiphikhoa->recursive = 0;
		$this->set('chiphikhoas', $this->paginate(array('ngaynhan >='=>$ngaybd,'ngaynhan <='=>$ngaykt,'Chiphikhoa.khoa_id'=>$khoa_id)));
		$this->set(compact('khoa_id','namhoc','dpht','ngaybd','ngaykt'));
		$this->set('khoa_id',$this->Session->read('khoa_id'));
	}
	
	function updatethongke($khoa_id=null)
	{
		$this->layout='ajax';
		if($this->data['Chiphikhoa']['ngaybd']=='')
			$this->data['Chiphikhoa']['ngaybd'] = date('d-m-Y');
		if($this->data['Chiphikhoa']['ngaykt']=='')
			$this->data['Chiphikhoa']['ngaykt'] = date('d-m-Y');
			
		$ngaybd = date('Y-m-d',strtotime($this->data['Chiphikhoa']['ngaybd']));
		$ngaykt = date('Y-m-d',strtotime($this->data['Chiphikhoa']['ngaykt']));
		$this->Chiphikhoa->recursive = 0;
		$this->set('chiphikhoas', $this->paginate(array('ngaynhan >='=>$ngaybd,'ngaynhan <='=>$ngaykt,'Chiphikhoa.khoa_id'=>$khoa_id)));
		$this->set(compact('khoa_id','namhoc','dpht'));
	}
	
	function xuatexceltk($khoa_id=null,$ngaybd=null,$ngaykt=null)
	{
		$this->layout='';
		$ngaybd = date('Y-m-d',strtotime($ngaybd));
		$ngaykt = date('Y-m-d',strtotime($ngaykt));
		$querystr = "	select sum(Tgdongdoanphikhoa.sotien) as doanphi
							from tgdongdoanphikhoas as Tgdongdoanphikhoa
							where Tgdongdoanphikhoa.khoa_id=".$khoa_id."
							and  ngaydong >='".$ngaybd."' and ngaydong <='".$ngaykt."'";
		$doanphinht = $this->Chiphikhoa->query($querystr);
		
		$dpht = isset($doanphinht[0][0]['doanphi'])?$doanphinht[0][0]['doanphi']:0;
		$chiphikhoas = $this->Chiphikhoa->find('all',array('conditions'=>array('ngaynhan >='=>$ngaybd,'ngaynhan <='=>$ngaykt,'Chiphikhoa.khoa_id'=>$khoa_id)));
		$this->set(compact('chiphikhoas','khoa_id','namhoc','dpht','ngaybd','ngaykt'));
	}

	function xuatexcelthuchi($khoa_id=null,$namhoc=null)
	{
		$this->layout='';
		$nhoc = $this->Namhoc->find('list',array('conditions'=>array('id'=>$namhoc),
        									'fields'=>array('id','namhoc')));
		$thangs=$this->Thang->find('list',array('fields'=>array('id','thang'),
                'conditions'=>array('namhoc_id'=>$namhoc),
				'order'=>array('id ASC')));
		if(!empty($thangs))
		{
        	$thang_id = array_keys($thangs);
		}
       	else
       	{
        	$thang_id= array();
       	}
       	if(!empty($thangs))
       	{
			$nams = split('-',$nhoc[key($nhoc)]);
		     $thang_d = split('-',$thangs[$thang_id[0]]);
	        	$thang_dau  =$thang_d[0];
				$thang_c = split('-',$thangs[$thang_id[count($thang_id)-1]]);;
	        	$thang_cuoi = $thang_c[0];
	        	$ngaybd =date('Y-m-d',strtotime($nams[0].'-'.$thang_dau.'-1'));
	        	$ngaykt='';
		    for($i=31;$i>=26;$i--)
		    {
		    	if(checkdate($thang_cuoi[0],$i, $nams[1]))
				{
		        	$ngaykt = date('Y-m-d',strtotime($nams[1].'-'. $thang_cuoi[0].'-'.$i));
		        	break;
		        }
		    }
		    $querystr = "	select sum(Tgdongdoanphikhoa.sotien) as doanphi
							from tgdongdoanphikhoas as Tgdongdoanphikhoa
							where Tgdongdoanphikhoa.khoa_id=".$khoa_id."
							and  namhoc_id=".$namhoc;
			$doanphinht = $this->Chiphikhoa->query($querystr);
			$dpht = $doanphinht[0][0]['doanphi'];
			$this->set('chiphikhoas', $this->Chiphikhoa->find('all',array('conditions'=>array('ngaynhan >='=>$ngaybd,'ngaynhan <='=>$ngaykt,'Chiphikhoa.khoa_id'=>$khoa_id))));
			$this->set(compact('khoa_id','namhoc','dpht'));
       	}	
	}
	
	function bieudo($khoa_id=null)
	{
		if(!$khoa_id)
			$this->redirect(array('controller'=>'pages','action'=>'home'));
		$chidoan_id = $this->Session->read('chidoanht_id');
		if(!empty($this->data))
		{
			$namhoc_id = $this->data['Chiphikhoa']['namhoc'];
		}
		if(!isset($namhoc_id))
		{
			$namhoc_id = $this->Session->read('namhocht_id');			
		}
		$this->Namhoc->displayField='namhoc';
		$namhocs = $this->Namhoc->find('list');
		
		// data
		$thangs=$this->Thang->find('list',array('fields'=>array('id','thang'),
                'conditions'=>array('namhoc_id'=>$namhoc_id),
				'order'=>array('id ASC')));
		if(!empty($thangs))
		{
        	$thang_id = array_keys($thangs);
		}
       	else
       	{
        	$thang_id= array();
       	}
       	if(!empty($thangs))
        {
        		$namhoc = $this->Namhoc->find('list',array('conditions'=>array('id'=>$namhoc_id)));
				$nams = split('-',$namhoc[key($namhoc)]);
	        	$thang_d = split('-',$thangs[$thang_id[0]]);
	        	$thang_dau  =$thang_d[0];
				$thang_c = split('-',$thangs[$thang_id[count($thang_id)-1]]);;
	        	$thang_cuoi = $thang_c[0];
	        	$ngaybd =date('Y-m-d',strtotime($nams[0].'-'.$thang_dau.'-1'));
	        	$ngaykt='';
	        	for($i=31;$i>=26;$i--)
	        	{
	        		if(checkdate($thang_cuoi, $i, $nams[1]))
	        		{
	        			$ngaykt = date('Y-m-d',strtotime($nams[1].'-'. $thang_cuoi.'-'.$i));
	        			break;
	        		}
	        	}
	        	//doan phi
	        	$querystr = "	select Tgdongdoanphikhoa.thang_id, sum(Tgdongdoanphikhoa.sotien) as sotien
							from tgdongdoanphikhoas as Tgdongdoanphikhoa
							where Tgdongdoanphikhoa.khoa_id=".$khoa_id."
							and  namhoc_id=".$namhoc_id." group by Tgdongdoanphikhoa.thang_id";
			$doanphinht = $this->Chiphikhoa->query($querystr);
	        	/*
	        	 * phi thu - chi nam hoc
	        	 */
	        	$phithus = $this->Chiphikhoa->find('all',array(
	        									'fields'=>array('DATE_FORMAT(ngaynhan,"%m")as thang','sum(Chiphikhoa.sotien) as sotien'),
	        									'conditions'=>array('ngaynhan >='=>$ngaybd,
	        														'ngaynhan <='=>$ngaykt,
	        														'khoa_id'=>$khoa_id,'loaichiphi_id'=>3),
	        									'group'=>array('month(ngaynhan)','loaichiphi_id')));
	        	$phichis = $this->Chiphikhoa->find('all',array(
	        									'fields'=>array('DATE_FORMAT(ngaynhan,"%m")as thang','sum(Chiphikhoa.sotien) as sotien'),
	        									'conditions'=>array('ngaynhan >='=>$ngaybd,
	        														'ngaynhan <='=>$ngaykt,
	        														'khoa_id'=>$khoa_id,'loaichiphi_id !='=>3),
	        									'group'=>array('month(ngaynhan)','loaichiphi_id')));
	        	
				$str = 'select max(a.sotien) as MAX
						FROM (SELECT sum(`Chiphikhoa`.`sotien`) as sotien 
	        			FROM `chiphikhoas` AS `Chiphikhoa`  
	        			WHERE `ngaynhan` >="'. $ngaybd .  '" AND `ngaynhan` <="'.$ngaykt.'" AND `khoa_id` ='.$khoa_id 
	        			.' GROUP BY month(ngaynhan), loaichiphi_id) a';
	        	$result = $this->Chiphikhoa->query($str);
	        	$max = $result[0][0]['MAX'];
	        	
	        	$thangs = $this->Thang->find('list',array('conditions'=>array('namhoc_id'=>$namhoc_id),'fields'=>array('thang')));
	        	$result = array();
	        	foreach ($thangs as $key=>$thang)
	        	{
	        		$result[$key]['thang']=$thang;
	        		$thg  = split('-', $thang);
	        		foreach ($phichis as $phi)
	        		{
	        			if($thg[0] == $phi[0]['thang'])
	        				$result[$key]['phichi']=$phi[0]['sotien'];
	        		}
	        		foreach ($phithus as $phi)
	        		{
	        			if($thg[0] == $phi[0]['thang'])
	        				$result[$key]['phithu']=$phi[0]['sotien'];
	        		}
	        		
	        		if(!isset($result[$key]['phithu']))
	        			$result[$key]['phithu']=0;
	        		if(!isset($result[$key]['phichi']))
	        			$result[$key]['phichi']=0;
	        		foreach ($doanphinht as $doanphi)
	        		{
	        			if($doanphi['Tgdongdoanphikhoa']['thang_id']==$key)
	        			{
	        				$result[$key]['phithu']+=$doanphi[0]['sotien'];
	        				if($max<$result[$key]['phithu'])
	        					$max = $result[$key]['phithu'];
	        			}
	        		}
	        	}
        }
        if($max==0)
        	$max=10;
		$this->set(compact('namhocs','namhoc_id','max','result','khoa_id'));
	}

}
